// pages/shopList/shopList.js
var request = require('../../utils/https.js');
var uri = 'user/binding_shop.html';
Page({

  /**
   * 页面的初始数据
   */
  data: {
      keyWords:'',
      shopList:[],
      page: 1,
      pageSize: 10,
      allPages: '',    // 总页数
      loadtext: '加载更多……',
      hideBottom: true
  },
  getList: function (key) {
    var that = this;
    if(key==undefined){
      key='';
    }
    var pageIndex = that.data.page;
    request.req(uri, {
      user_id: wx.getStorageSync('userId'),
      page: that.data.page, 
      page_size: that.data.pageSize,
      keywords:key
    },
      (err, res) => {
        if (res.data.status) {
          var list = [];
          if (res.data.data != '没有数据') {
            for (var i = 0; i < res.data.data.length; i++) {
              list.push(res.data.data[i]);
            }
            setTimeout(function () {
              that.setData({
                hideBottom: true,
                loadingText: '加载中...'
              })
            }, 2000)
          } else {
            that.setData({
              hideBottom: false,
              loadingText: '已无更多数据'
            })
          }
          if (that.data.page == 1) {
            that.setData({
              allPages: res.data.count,
              shopList: list,
              hideBottom: true,
            })
          } else {
            var nowList = that.data.shopList;
            nowList = nowList.concat(list);
            that.setData({
              allPages: res.data.count,
              shopList: nowList,
              hideBottom: false,
              loadingText: '加载中...',
            })
            setTimeout(function () {
              that.setData({
                hideBottom: true,
                loadingText: '加载中...'
              })
            }, 2000)
          }
        } else {
           wx.showModal({
             title: '错误',
             content: '获取商家列表失败，请重新获取！',
           })
        }
      })
  },
  keyWordsInput:function(e){
    this.setData({
      keyWords: e.detail.value
    })
  },
  searchShop:function(){
    var keyWords = this.data.keyWords;
    console.log(keyWords);
    if(keyWords==''){
      wx.showToast({
        title: '请输入商家姓名',
        icon: 'none'
      })
    }else{
      this.getList(keyWords);
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getList();
    this.setData({
       shopList:[
         {
           id:1,
           name:'小强',
           tel:'18990653167',
           address:'四川成都市环球中心',
           status:1
         },
         {
           id: 2,
           name: '小强',
           tel: '18990653167',
           address: '四川成都市环球中心',
           status: 2
         },
         {
           id: 3,
           name: '小强',
           tel: '18990653167',
           address: '四川成都市环球中心',
           status: 3
         }
         ]
    })
  },
  onPullDownRefresh: function () {
    var that = this;
    this.setData({
      page: 1
    })
    this.getList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    this.setData({
      page: that.data.page + 1
    })
    this.getList();
  },
})